So today we are continuing off looking at if statements.
Okay, so last lesson we started off and we learned about well what they're used for.
Why we need two equal signs here.
Um how we can shorten down writing um boolean data types okay.
And how to use multiple at once.
Okay.
Um, we're going to extend these today and look at two additions you can make to your if statements
called else ifs and elses.
Okay.
So I would recommend that you've had a good practice at if statements first before you watch this as
we're basically taking them and making them a bit more advanced.
Okay.
So let's write a very simple if statement to start off with.
Okay.
So if part one.
Dot.
Um.
Dot transparency is equal.
Equal one.
Okay.
And then press.
Then enter.
Okay.
So um this here will run some code if the part is completely transparent.
Okay.
Um.
Uh, or should we do a different one?
Um.
Note that should be fine.
That should be fine.
Okay, so this here, we know we'll run some code.
If the part is completely transparent.
Okay.
Um.
Now, let's say you wanted to, um, you wanted to write a second one.
And let's say this one, um, checks if, um, it has a certain material.
So let's say if part one dot material is equal to enum is equal.
Equal even.
I make the mistake of forgetting sometimes enum dot material.
Dot neon, for example.
But we also wanted to check if this part is transparent.
Is not transparent.
Sorry.
So.
And part one dot transparency does not equal one.
Okay.
Because obviously if it's invisible we can't see the material.
Okay.
Um well you can combine these two into one larger if if statement.
Okay.
Um, you can do this by using an else if.
So you first write the code you want.
I'm going to I'm just going to print these.
Use a print function to be a bit easier.
So we're going to print part is transparent okay.
If a transparency is one okay.
After that drop a line press backspace to get to the start of it.
Then write the keyword else if okay and then write the space.
So else if basically allows you to write a second condition.
Okay, so let's write a second condition.
This is going to be the material being neon.
So part one dot material is equal equal enum dot material dot neon.
So you write the same things and you still put then afterwards.
So it's very similar.
It's just follow straight afterwards a before we end and you write else before you write the if.
Okay.
Now the difference between writing if and else if okay is based on it's based on what happens to the
first condition.
So let's say this runs okay.
And um the transparency is one okay.
If transparency is one it will print this.
And remember it ignores the rest of it.
It ignores the rest of the if statement.
Okay.
So um, if this is true, it will not read the else if it only reads the else if if the first condition
is not met okay.
It's not it's not true.
So if a transparency is not one then it will read the else if okay.
So if this if this condition is true the code written here will run.
And if this code runs we will know.
But we will obviously know that the material is neon.
Okay.
Material is neon.
Oh, neon.
Um, but we will also know that the part is, um, not transparent.
Okay.
The reason why we know that is because if the part was transparent, if it had a if it had a transparency
of one, it would run this print function and it would ignore the rest of my if statement.
Okay.
So it only runs one bit of code.
And as soon as it's run that bit of code, it ignores the rest of it.
So writing this here is the exact same as writing this, because you're also checking if a transparency
is one or not equal to one, because you're previously checking if it is one.
Okay, um, hopefully that makes sense.
And you can include as many else ifs as you'd like, which makes it much more precise what you're checking,
because you're not only checking for that one condition, but you're also checking for all the previous
conditions.
Okay.
Um, so you can write as many steps as you like as long as they come before.
Um, sorry.
Not before.
Like they come after the your first if.
Okay.
You can't include else if before you have an if because the else if is all about um what if the if does
not happen basically.
Okay.
Um, but there's another type just called an else okay.
So drop a line backspace and let's write else.
Okay.
And then you actually just press enter after that okay.
So when you write an else in your if statement, the else doesn't have a doesn't have a, you don't
write a condition for the else okay.
It's basically um.
If none of your conditions were met.
So Britain's bouncy was not equal to one and the material was not equal to neon, then it will run the
else.
So if you'd like to think about it this way, the condition for else is all the other conditions not
being met.
Okay, so that something will always happen no matter what.
Okay, so if the else runs, we know two things, okay?
We know that that that's the material is not neon okay.
Because if it was neon it would, it would run this bit of this bit of code here.
Okay.
And we also know that the part is not transparent because if it was transparent it would run this bit
of code here.
Okay.
So that's your example there of using else ifs and elses in an if statement.
Okay.
It's quite a lot to learn at once, but hopefully it does make sense.
All you're doing is just checking is just is just checking something else.
But only if what happened previously did not work okay.
Which basically means that whenever you use an if statement, some code will always, always, always
run no matter what, which is quite useful for the race example I used last lesson where you might want
to display some confetti.
If the player wins.
If the player doesn't win, they might play second, in which you might also want to do something if
they place third, you might also want to do something.
If they place last, you might want to do something as well.
Okay, so that's a really good way to check multiple things in 1 in 1 if statement instead of separating
it into multiple.
Okay.
Um, now now obviously you can use um v signs and the you can use multiple conditions as well for the
IFS and the IFS.
I didn't use them because it will make them a bit complicated, I think to start to start off with as
your first example, but I'd encourage you to try out some of these signs and using multiple conditions
with this as well as I hope you can start to see now, I can't teach you, um, not equal to greater
than, less than or these signs and all these conditions and stuff.
It's just too many combinations and molesting to be like hours long.
So it's up to you to try some things out with those.
Okay.
And next lesson we are going to start looking at one of the three different loops.
We're going to look at.
So loops similar to statements but they have a bit of a different function.
So join me in the next lesson to start learning about loops.